package com.sjzd.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sjzd.server.pojo.Admin;
import com.sjzd.server.pojo.RespBean;
import com.sjzd.server.pojo.Role;
import com.sjzd.server.service.IAdminService;
import com.sjzd.server.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sjzd
 * @since 2021-02-21
 */
@Api("admin控制器")
@RestController
@RequestMapping("/sys/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @Autowired
    private IRoleService roleService;

    @ApiOperation(value= "获取所有普通操作员")
    @GetMapping("/")
    public List<Admin> getAllAdmins(String keywords) {
        return adminService.getAllAdmins(keywords);
    }

    @ApiOperation(value= "获取当前登录用户的信息")
    @GetMapping("/info")
    public Admin getAdminInfo(Principal principal) {
        if (null == principal) {
            return null;
        }

        String username = principal.getName();
        Admin admin = adminService.getAdminByUsername(username);
        admin.setPassword(null);
        admin.setRoles(adminService.getRolesByAdminId(admin.getId()));
        return admin;
    }

    @ApiOperation(value="获取所有角色信息")
    @GetMapping("/roles")
    public List<Role> getAllRoles() {
        return roleService.list();
    }

    @ApiOperation(value="删除操作人")
    @DeleteMapping("/{id}")
    public RespBean deleteAdmin(@PathVariable("id") Integer id) {
        if (adminService.removeById(id)) {
            return RespBean.success("删除成功！");
        } else {
            return RespBean.error("操作失败！");
        }
    }

    @ApiOperation(value="更新用户状态")
    @PutMapping("/")
    public RespBean changeEnabled(@RequestBody Admin admin) {
        if (adminService.updateById(admin)) {
            return RespBean.success("更新成功！");
        } else {
            return RespBean.error("更新失败！");
        }
    }

    @ApiOperation(value="更新用户角色信息")
    @PutMapping("/{id}/{rids}")
    public RespBean changeRoles(@PathVariable("id") Integer id, @PathVariable("rids") Integer[] rids) {
        return adminService.updateRolesById(id, rids);
    }
}
